<?php


class Apigoods extends ApiBase {
	public function index(){
		
		 $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		 $goods=$this->cache->get('goods');
		 if (!$goods){
        $where['goods_status']=10;
		$list=$this->db->select('goods_id,goods_name,desc')->where($where)->order_by('goods_sort')->get('goods')->result_array();
        $this->cache->save('goods', $list,86400);
        }else {
        $list=$goods;
        }
	    foreach ($list as $k=>$v){
			$img=$this->GetGoodsImage($v['goods_id']);
			if (!empty($img)){
				$list[$k]['image']=$this->GetImg($img[0]['image_id']);
			}else{
				$list[$k]['image']='';
			}
				$list[$k]['goods_price']=$this->getPrice($v['goods_id']);
		}

	    $map['status']=10;
	    $picture=$this->db->select('img_id')->where($map)->or_where_not_in('id',[1,2])->order_by('sort')->get('picture')->result_array();
		foreach ($picture as $k=>$v){
			$picture[$k]=$this->GetImg($v['img_id']);
		}
		$this->show(array('goods'=>$list,'picture'=>$picture));
	}

    


	public function detail(){
		$where['goods_id']=(int)$this->uri->segment(3);
		$list=$this->db->select('goods_id,goods_name,desc,content')->where($where)->get('goods')->row_array();
		$list['image']=$this->GetGoodsImage($list['goods_id']);
		foreach ($list['image'] as $k=>$v){
			$list['image'][$k]=$this->GetImg($v['image_id']);
			$list['goods_price'][$k]=$this->getPrice($v['goods_id']);
		}

		$map['goods_hot']=10;
		$hot=$this->db->select('goods_id,goods_name')->where($map)->get('goods')->result_array();
		foreach ($hot as $k=>$v){
			$img=$this->GetGoodsImage($v['goods_id']);
			if (!empty($img)){
				$hot[$k]['image']=$this->GetImg($img[0]['image_id']);
			}else{
				$hot[$k]['image']='';
			}
				$hot[$k]['goods_price']=$this->getPrice($v['goods_id']);
		}
		
		$sku=$this->db->select('id,name,price,weight')->where($where)->get('goods_sku')->result_array();
		foreach($sku as $k=>$v){
			$sku[$k]['name']=$v['name'].'('.$v['weight'].')';
		}
        $this->show(array('list'=>$list,'hot'=>$hot,'sku'=>$sku));
	}


}
